package com.feicheng.iou.controller;


import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.Role;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.login.AddRolePOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.RoleService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;  //角色业务实现类
    /**
     * @Author wuweinan
     * @Description //查询所有角色接口
     * @Date 2021/7/28 11:41
     * @Param null
     * @return ResultInfo
     */
    @GetMapping("/list")
    public ResultInfo queryAllRoles(){
        return  roleService.selectAllRoles();
    }

    /**
     * @Author wuweinan
     * @Description //查询单个角色和其权限接口
     * @Date 2021/7/28 11:41
     * @Param null
     * @return ResultInfo
     */
    @GetMapping("/info/{roleId}")
    public ResultInfo queryRoleByRoleId(@PathVariable(value = "roleId") Integer roleId){

        log.info("RoleController的queryRoleByRoleId方法进入，参数roleId：" + roleId);

        //校验参数是否为空
        if (roleId == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return roleService.selectRoleByRoleId(roleId);
    }
    /**
     * @Author wuweinan
     * @Description //新增角色和其权限接口
     * @Date 2021/7/28 11:41
     * @Param null
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addRole(AddRolePOJO addRolePOJO){
        log.info("RoleController的addRole方法进入，参数addRolePOJO：" + addRolePOJO);

        //判断请求是否重复
        if (!IdempotentUtils.judge(addRolePOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"新增角色"));
        }


        //校验参数是否为空
        if (addRolePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }
        return roleService.insertRole(addRolePOJO);
    }
}

